package com.wjm.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wjm.entity.Prestige;
import com.wjm.entity.Student;
import com.wjm.service.impl.PrestigeServiceImp;
import com.wjm.service.impl.StudentServiceImpl;
import com.wjm.util.CacheUtil;
import com.wjm.util.StringUtil;

@Controller
@RequestMapping("/web/stu")
public class StudentWebController {

	@Resource(name = "studentService")
	private StudentServiceImpl studentService;

	@Resource(name = "prestigeService")
	private PrestigeServiceImp prestigeService;

	private final String listKey = "list";
	private final String pagesKey = "pages";
	private final String pageKey = "page";
	private final String statusKey = "status";

	@RequestMapping("/query")
	public String query(int page, int status, Model model) {
		String result = "";
		try {
			int number = Integer.parseInt(CacheUtil.getProperties().getProperty(StringUtil.STU_NUM));
			int total = studentService.queryTotal(status);
			int pages = total % number == 0 ? (total / number) : (total / number + 1);
			List<Student> list = null;
			if (page > 0 && page <= pages) {
				list = studentService.queryByPage(page, number, status);
			} else {
				list = new ArrayList<>();
			}
			model.addAttribute(listKey, list);
			model.addAttribute(pageKey, page);
			model.addAttribute(pagesKey, pages);
			model.addAttribute(statusKey, status);
			result = "/web/stu/stu-show";
		} catch (Exception e) {
			e.printStackTrace();
			result = "error";
		}
		return result;
	}

	@RequestMapping("update")
	@ResponseBody
	public String update(int stuId, int status) {
		String result = "";
		try {
			if (studentService.updateStatus(stuId, status)) {
				result = "{\"result\":\"success\"}";
			} else {
				result = "{\"result\":\"fail\"}";
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "{\"result\":\"error\"}";
		}
		return result;
	}

	@RequestMapping("/search")
	public String query(String stuNo, Model model) {
		String result = "";
		try {
			Student student = studentService.queryByIdOrNo(0, stuNo);
			List<Student> list = new ArrayList<>();
			list.add(student);
			model.addAttribute(listKey, list);
			model.addAttribute(pageKey, 1);
			model.addAttribute(pagesKey, 1);
			model.addAttribute(statusKey, student.getStuStatus());
			System.out.println(student.getStuStatus());
			result = "/web/stu/stu-show";
		} catch (Exception e) {
			e.printStackTrace();
			result = "error";
		}
		return result;
	}

	@RequestMapping("/reset")
	@ResponseBody
	public String reset(int stuId) {
		String result = "";
		try {
			if (studentService.reset(stuId)) {
				result = "{\"result\":\"success\"}";
			} else {
				result = "{\"result\":\"fail\"}";
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "{\"result\":\"error\"}";
		}
		return result;
	}

	@RequestMapping("/info")
	@ResponseBody
	public Prestige info(String stuNo) {
		Prestige pre = null;
		try {
			pre = prestigeService.queryByStuNo(stuNo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pre;
	}
}
